added tests, fixed bug in sorting logic