Removed unused import of pickle
Added another alternative definition of user-defined switch function