Added a for loop and fixed some errors