Corrected a randomization bug
Refactoring
Minor improvements