Final tweaks and refactoring

Final tweaks and refactoring adding additional helper functions.