Adding treding and sorting from database