Added the right sorting mechanism in.