Make pivot table generation more robust