Lots of bug fixes

Fixes #3