tidied up logic jquery