Cleaned up loops a bit and put iterator variables at the top