refactored script parsing logic