Loop bug fix