add structure for phi partitioning and cleanup of code