refactor script and rebuild