Various bug fixes

Addresses #60, #54, #45 and #49.

Review: @jbalogh