fixed overwriting error by implementing proper database shutdown.