code cleanup and script fix