Fixes #333 (cherry-picked from master)