Added transaction support when moving data to and from database. Also minor refactorings.