Updated solution without code duplication