Heat engines and heat pumps