Initial working code with persistence