modularized algos into a simulation class and made more readable