Refactored script generation and cached
Refactored