refactor code to make it more testable + add spec