Updated patch with comments from Hannes' review.