Script general improvements and refactor