clean up script and bug fixes