use transaction

This is quick hack.  Please check carefully.