restructuring javascript