Improves sorting

Also adds more documentation to make this easier for anybody who wants
to try and pick up where I left off.