backporting some fixes from dev branch