Minor refactoring and added tests