Fix #118 against 4-0-stable

Fix for #118 against 4-0-stable.

This is really mostly @ofavre's work from awhile ago, I just updated it for 4-0-stable.