Overall refactoring with added unit tests (4)