Prim implementation