optimize code repetitions