Updated main with state maintenance