Bugfix to maindb code