v0.1, first write of the script
various fixes and improvements after intensive testing