updated javascript logic