Added more tests, fixed bugs and factored some classes into separate files.