Refactoring onBeginRequest