some javascript that earns to be refactored