Rewrite issue48

Issue #48 backported to rewrite branch.