added for loops and fixed variable initialization