simplify and remove duplicate code