Refactored a little bit of logic.