Added state and writeout functionality