Fix a bug in sampling, add weights to edges and do some cleanup and tuning.