working on new nmd code. Original too slow