Add rspec tests and refactor