code refactoring and adding test