Version after some refactoring by Matt, John and myself