added and merged minmax residual density branch