added tests, clean up code