big rewrite of NXT program using loops and without threads
various fixes