script: various refactorings