Fixed isEmpty function/tidied up code