refactor the select() function