Various code tweaks and refactorings suggested by IntelliJ