Fixes issues #368 and #341

Can be reviewed and merged quickly. @imartinez may want to take care of this one