Fixed typo and refactored a few more loops.