Minor refactoring, moving functions around