changes to allow this code to be used as a module