Refactored to clean up; replaced rand() with mt_rand().