clarify code for ties, reorder functions for clarity