Refactory javascript