code to handle context switching