copy and small refactor from p2p-naive