Fix various bugs with managing contexts throughout the application.