added to db query and added sorting features