Cleaned up code that handles randomSwitch