refactoring, tests for some edge cases