Use faster ruby methods

Swap some methods to their faster equivalents (hash.keys.each and hash.fetch). See https://speakerdeck.com/sferik/writing-fast-ruby