pieces refactoring and tests