merged patches from https://sourceforge.net/tracker/?func=detail&atid=478072&aid=2947822&group_id=55736