Code refactor moved RNFO and RNTO into one call.

Do not need to keep rnfo and rnto in different functions.
Also we removed a lot of global variables making the code
look nicer.