Bug fixes

Fixes some of the bugs I've mentioned on IRC. More to come.