Remove old unused random module.
Moved some code around; hopefully things are more readable.