Refactored the toggling code