fix random and some refactor