adding MST using Prim's algorithm and doing some refactoring