Better sorting code, fix logout destroying all sessions