added tests and cleaned up code