Add search to API. refactor code