Corrected various bugs - including resettting n_ports to correct value