Refactored couchdb stuff from calvad_grid_merge