Refactored to prepare for graphs