Fixed problem with sorting, added some test cases