fixed database crashes and sorting