apply FLOZi's  patch (with minor edits)