Fixed bug with DB query