Break code into functions and put in external js file