Fix random bug and test