some cleanup and bug fix in context cache