Misc. script refactoring