duplicate code refactored to functions