Fix lint errors

This should fix every lint error, targeting Ruby 1.9.3. Tests are passing but given test coverage I'm not sure if that's much of a guarantee for it working properly, that said I think this should be fine.
Decomposed method to reduce cyclomatic complexity.