Fix bugs with deleting and querying database