Some code tweaks and refactoring.