revised the code to be a lot better (use less pickles/unpickles)