modify structure of algorithm, reduce memory cost