Fixing database function