code cleaning via rubocop and error handling