refactored to use function