refactored with tests
v2 works