Miscellaneous bug fix

Several important bugs are fixed. Among them, the errorneous behaviour
of the random selector is fixed.