Misc bug fixes. See description

Fixes #21
Fixes #20
Fixes #19