Final changes to the code imported from Bert & Philip's efforts