Fixed bug from database function