backport bugfixes from gotham branch