Fixes #408

Includes the features mentioned in the comment for #408