refactor to reduce indentation level