event sorting, cleaned up a lot