changes: 1) it fixes crashes with empty/near-empty databases 2) it fixes a few performance bottlenecks, most notably for larger databases