refactored transactions, fixed a memory leak