Added check on remove and tidied up some code