Re-factored, mostly due to Rubocop.