refac js code