Again, lots of logic in here specific to pig. Can this be abstracted out?