Fix database logic