Refactored loops to be more consistent with other files and slightly
more optimized.