Refactored some internal methods to clean up some ugly code.