Tidied up for Pascal version