Move more code out of createTx() into a separate function

Also adds comments for new functions/methods