Added function handleContext to improve code reuse while refactoring (Feature Request #1466)