Tidy up selection code, fixed minor selection bugs when adding and deleting