refactored the code some.