sorting related code improvements