Replace 35 instances of the same code with a subroutine.
Fix some incorrect comments.