Merged in changes from @gerryhigh - with a few modifications