Refactor and add rspec coverage