fixed for loops in algo