More refactoring of the function.