Major refactoring of scripts