Overall refactoring with added unit tests (2)