refactored big chunk into helper method