Move around some BEGIN and COMMIT commands to avoid misnested transactions in some cases