Added basic piece manager and some refactoring in peer code