refactoring sorting, fixes #61