Added tests
Clean up code slightly