updated according to Chris's comments. Now passes all unit tests