various fixes and improvements on script