Refactoring: In multiple locations, remove one level of nesting to return earlier, declare variables sooner, and avoid inline if statements.
Refactor:
Avoid single entry, single exit where it's not needed (http://programmers.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from)
Braces, readability
Minor cleanup