Refactoring of context access and some cleanup.