Improved exit logic