Fixed rubocop offenses and rearranged some code. Added a bit more documentation.