Bug fixes

See commits for more info