code refactoring to reduce indentation and improve comprehension