some refact, ready to merge into master