Porting over sorting fix from client.js