commented code and fixed looping