Resolves #1284. Merge patch submitted a bit differentely