fixed bug; update reg for every iteration