Moved sort code