Refactoring of get context function