Minor script and code refactorings