better loop handling