Some refactoring and region fixing to make it easier to identify sections.