Sorting out JS bug