Minor refactoring and added unit tests