refactors, adds unit tests