Refactored code so I could use internal functions.