refactored to be much simpler.