adding rspec test and refactoring code