Bug fixes

See commit log for details.