Some bugfixes

Hi there, a few minor changes here.  The only thing that may be problematic is there's a large 'cleanup' commit that standardises the whitespace and adds missing semicolons.  This may break merging, so let me know if that's the case and I'll rebase and reissue the pull request.

Cheers!