Ported to use arrays instead of graphs