Fixed some database code. Added in some tests.