properly separate functions in javascript