Improved for loop and corrected error pointed out by @bobeckert