factored out some code, added random number generator