Backported some changes from another project