Bit of refactoring, added some comments for cleaning up functions and persisting data to database