Script optimization and correction of bugs