Refactor to make less a direct port of python shelve but still keep the spirit of the code.