Improved comments. Added randomization function and parameter processing.