Remove some unneeded cruft and change rank type to unsigned long to save 4 bytes.
Use some more efficient data structures to save memory.
Tighten up visibility and add some tests that don't pass yet to do with
adding cleanups.