Backport imemdb bugfixes