Refactor scripts preparation code