Added logic to support in-memory and permanent database options

Take a look guys and let me know if you see any issues.