Further cleanup lib.php and core_renderer for correct context definition and reduce NPath complexity