Calculating heat transfer and work