Refactored implementation of graphs