Extract nested functions into proper methods in application.js