rewrite with the major functionality broken out into a class so we can reuse it better