some reafactoring and tests