Support IndexedDB and refactor the code