trying to optimize duplicate code