Reorganizing javascript for reusability