Updated javascript logic for proper gameplay