rework and clean up complete script