Added correct sorting to some queries