added a factory. cleaned up code.