Added some more detailed tests, and cleaned up code