Move loop check to the front