Clean up context switching.

Added an enum and a switch to make the context-switching code more
readable.