formatted code and added print statement for step-by-step iterations