script cleanup/refactor