Refactored search.js code