code refactor and rspec tests