cleaner implementation with a simple Graph class