Is this needed?  ApplicationController itself seems to respond to main_app correctly, and in fact, you're relying on it in your changes to application_controller, so maybe always just call main_app.