Refactor to reduce nesting