partial merge of update by Victor Ryzhykh with my corrections