Add some tests

Also a little refactoring and lots of PEP8 fixes.