refactored to use functions