refactored the generation of reduction codes (tested)