Merge some of Mahmut's patches