Re-arrange JS for if checks