Improved script building and serialization