refactor db and sort