refactored with functions