Bit more refactoring to make the implementation clearer