Applied #1441 to master

This adds the fixes from #1441 to master as well.