Added rspec tests and improved code