added random logic. Needs more testing