Bug fixes in sorting added