Pulled fix for endless loop from e851. Minor clean up of redundant code for opening databases.