tidy up db access a bit, fix bug that inputs/outputs/transactions where in revers order