Apply fixes made upstream in pull 425 by kwart (jcacek)