Fix for Feature Request 785. Updated with comments and sanitization