optimize the rnm function