functional version with search added