improved sorting, a little hacky