Factored out some code into separate methods for main js.