refactor to reduce code complexity