updated to use seperate thread for persisting the data
Some initial refactoring