Update unit tests and patch small issues.
Fix sorting bug.