Tidied up NMS code