Manual merge of #412 and #417.