some refactoring and also progress on bayes