Applied patch with the previous PR and fixed some merge problem