Refactored some stinking loops.