Gh 1449

Rebase of PR #1449, with style fixes, courtesy of inspekt :)