Improved loop handling