Fix ordering bug

I messed up the original algorithm slightly...partially because of a bug in my own test code. This fixes the bug as well as adds a guard against incorrect usage.