added sqlite persistence and refactored stats