This line will be executed in every loop, so you can move it out of the if block and remove line 545 and the else block.
After that it is also possible to join line 541 and 542 to a single if statment