Some optimizations to context switch code