Refactor to try to clean up the code some.