More refactoring of functions into smaller bits