Add p_master and do some refactoring