Javascript restructuring