adds unit tests and refactors