Many changes to use more object oriented code,i.e. cleanup and exception handling