Function sorting bug