Bugfixes

A bunch of bugfixes that was suggested by @cevich.