adding unit tests and small refacto