almost complete rewrite (allow more inut and output ports in blocks)