Sort on query, instead of afterwards. Clean up related test