Refactored DB transaction code to control structure