move script loading logic out of layouts

and into app_controller