Fixing implicit ordering logic, and adding tests.