Identified common part of code to refactor. created method handleContext in util.class.php to deal with this.