Moved context functionality into function