Much modification, mainly to allow the script to be called as a library from another script.

modifications in the string required and the way it is searched.