Overhauled state management code for lights