Tidy redundant code for javascript