Extract script logic into script.js