Improved database logic