Added thermalize function. Working version.