Move pickling to handle pickling error properly