What happens to a gas as it loses thermal energy?