Fix Issue 14718 and 17920

Code as provided by Sander Hollaar in http://projects.puppetlabs.com/issues/14718
Problem is also described in http://projects.puppetlabs.com/issues/17920