setting up sorting, bug fixes and cleanup