some bugfixes, sync with upstream