refactor to simplify code