adding some functions from Peter Kovesi