Improve code quality

This'll probably be a continuous work in progress, but let's call this a start.

Issue types fixed:
- Newlines at end of files
- Reduced code duplication in several places
- Reduced nesting of code by splitting methods out into separate functions
- Reduced DOM queries by caching selectors
- Fixed a few other linting issues
- Fixed missing radix parameter on `parseInt`