refoactored javascript