More refactoring (work in progress)
More work in progress
Moved code out of helper functions (work in progress)