More refactoring, it finally works