Bug fixes to database

Use this instead of the previous one as that one had a few bugs