updated search function to fix scoping issue