Putting output statements to check results at every step usually helps.