added rspec tests and refactored the code a b it