Bug fixes. Thanks to the contributors: @doak