This is the modified version of former jl6583 commit