removed some rubbish, tidied up logic a little