Bug fixes and some restructuring in PbMap