Betterifies the code

Fixes some of the problems mentioned in the comments.