Refactored code to reduce necessity of restarting