implemented tests and refactored some code