Re-factor out common partition code improve comments