added retain scratch option, cleaned compiler errors and code cleanup