bug fixes to allow running as script