diff -ur espresso-5.1_pristine/Modules/read_cards.f90 espresso-5.1/Modules/read_cards.f90
--- espresso-5.1_pristine/Modules/read_cards.f90	2014-05-28 07:43:40.000000000 -0700
+++ espresso-5.1/Modules/read_cards.f90	2014-10-10 12:20:08.787574000 -0700
@@ -768,8 +768,15 @@
             !  generate the points along the lines
             !
             CALL generate_k_along_lines(nkaux, xkaux, wkaux, xk, wk, nkstot)
+            !
+            !  workaround: discard current wk (contains the length of k-path, 
+            !  never used), replace with wk=1 so that band occupations (wg)
+            !  are correctly written to file - needed by BerkeleyGW interface
+            !
+            wk(:) = 1.0_dp
             DEALLOCATE(xkaux)
             DEALLOCATE(wkaux)
+            !
          ELSEIF (kband_plane) THEN
 !
 !        Generate a uniform mesh of k points on the plane defined by
diff -ur espresso-5.1_pristine/PP/src/bgw2pw.f90 espresso-5.1/PP/src/bgw2pw.f90
--- espresso-5.1_pristine/PP/src/bgw2pw.f90	2014-05-28 07:43:07.000000000 -0700
+++ espresso-5.1/PP/src/bgw2pw.f90	2014-10-10 12:16:05.656350000 -0700
@@ -96,9 +96,10 @@
 
   character (len=256), external :: trimcheck
 
-#ifdef __PARA
+#ifdef __MPI
   CALL mp_startup ( )
 #endif
+
   CALL environment_start ( codename )
 
   prefix = 'prefix'
@@ -132,6 +133,13 @@
   CALL mp_bcast ( rhog_file, ionode_id, world_comm )
 
   CALL read_file ( )
+
+  ! this is needed to compute igk and store in iunigk
+  ! cannot use gk_sort because for some k-points
+  ! gk_sort generates different igk on every call
+  CALL openfil ( )
+  CALL hinit0 ( )
+
   CALL openfil_pp ( )
 
   IF ( wfng_flag ) THEN
@@ -160,8 +168,12 @@
   IF ( rhog_flag ) CALL print_clock ( 'write_cd' )
 
   CALL environment_end ( codename )
+
   CALL stop_pp ( )
 
+  ! this is needed because openfil is called above
+  CALL close_files ( .false. )
+
   STOP
 
 CONTAINS
@@ -175,13 +187,14 @@
   USE constants, ONLY : eps6
   USE fft_base, ONLY : dfftp
   USE gvect, ONLY : ngm, ngm_g, ig_l2g, mill, g
+  USE io_files, ONLY : iunigk
   USE io_global, ONLY : ionode, ionode_id
   USE ions_base, ONLY : nat
   USE iotk_module, ONLY : iotk_attlenx, iotk_free_unit, iotk_open_write, &
     iotk_write_begin, iotk_write_attr, iotk_write_empty, iotk_write_dat, &
     iotk_write_end, iotk_close_write, iotk_index
   USE kinds, ONLY : DP
-  USE klist, ONLY : xk, nks, nkstot
+  USE klist, ONLY : xk, nks, nkstot, ngk
   USE lsda_mod, ONLY : nspin
   USE mp, ONLY : mp_bcast, mp_sum, mp_max, mp_barrier
   USE mp_world, ONLY : world_comm, nproc
@@ -189,10 +202,10 @@
   USE symm_base, ONLY : s, nsym
   USE xml_io_base, ONLY : create_directory
   USE qexml_module, ONLY : qexml_kpoint_dirname, qexml_wfc_filename
-#ifdef __PARA
+#ifdef __MPI
   USE parallel_include, ONLY : MPI_INTEGER, MPI_DOUBLE_COMPLEX
 #endif
-  USE wvfct, ONLY : npwx, g2kin, ecutwfc
+  USE wvfct, ONLY : npwx, g2kin, ecutwfc, igk
 
   IMPLICIT NONE
 
@@ -212,8 +225,7 @@
   character ( len = 256 ) :: filename
   character ( iotk_attlenx ) :: attr
 
-  integer, allocatable :: itmp ( : )
-  integer, allocatable :: ngk ( : )
+  integer, allocatable :: ngk_g ( : )
   integer, allocatable :: gvec ( :, : )
   integer, allocatable :: igk_buf ( : )
   integer, allocatable :: igk_dist ( :, : )
@@ -307,7 +319,7 @@
   ENDIF
   ngkdist_g = ngkdist_l * nproc
 
-  ALLOCATE ( ngk ( nk ) )
+  ALLOCATE ( ngk_g ( nk ) )
   ALLOCATE ( k ( 3, nk ) )
   ALLOCATE ( en ( nb, nk, ns ) )
   ALLOCATE ( oc ( nb, nk, ns ) )
@@ -328,7 +340,7 @@
     READ ( iu )
     READ ( iu )
     READ ( iu )
-    READ ( iu ) ( ngk ( ik ), ik = 1, nk )
+    READ ( iu ) ( ngk_g ( ik ), ik = 1, nk )
     READ ( iu )
     READ ( iu ) ( ( k ( ir, ik ), ir = 1, 3 ), ik = 1, nk )
     READ ( iu )
@@ -355,7 +367,7 @@
     ENDDO
   ENDIF
 
-  CALL mp_bcast ( ngk, ionode_id, world_comm )
+  CALL mp_bcast ( ngk_g, ionode_id, world_comm )
   CALL mp_bcast ( k, ionode_id, world_comm )
   CALL mp_bcast ( en, ionode_id, world_comm )
   CALL mp_bcast ( oc, ionode_id, world_comm )
@@ -390,13 +402,13 @@
         READ ( iu ) ( ( gk_buf ( ir, jg ), ir = 1, 3 ), jg = ig, ig + ng_irecord - 1 )
         ig = ig + ng_irecord
       ENDDO
-      DO ig = ngk ( ik ) + 1, ngkdist_g
+      DO ig = ngk_g ( ik ) + 1, ngkdist_g
         DO ir = 1, 3
           gk_buf ( ir, ig ) = 0
         ENDDO
       ENDDO
     ENDIF
-#ifdef __PARA
+#ifdef __MPI
     CALL mp_barrier ( world_comm )
     CALL MPI_Scatter ( gk_buf, 3 * ngkdist_l, MPI_INTEGER, &
     gk_dist, 3 * ngkdist_l, MPI_INTEGER, &
@@ -435,20 +447,20 @@
         ENDDO
         DO is = 1, ns
           IF ( real_or_complex .EQ. 1 ) THEN
-            DO ig = 1, ngk ( ik )
+            DO ig = 1, ngk_g ( ik )
               wfng_buf ( ig, is ) = CMPLX ( wfngr ( ig, is ), 0.0D0 )
             ENDDO
           ELSE
-            DO ig = 1, ngk ( ik )
+            DO ig = 1, ngk_g ( ik )
               wfng_buf ( ig, is ) = wfngc ( ig, is )
             ENDDO
           ENDIF
-          DO ig = ngk ( ik ) + 1, ngkdist_g
+          DO ig = ngk_g ( ik ) + 1, ngkdist_g
             wfng_buf ( ig, is ) = ( 0.0D0, 0.0D0 )
           ENDDO
         ENDDO
       ENDIF
-#ifdef __PARA
+#ifdef __MPI
       DO is = 1, ns
         CALL mp_barrier ( world_comm )
         CALL MPI_Scatter ( wfng_buf ( :, is ), ngkdist_l, MPI_DOUBLE_COMPLEX, &
@@ -491,7 +503,7 @@
     DEALLOCATE ( wfngc )
   ENDIF
 
-  CALL mp_bcast ( ngk, ionode_id, world_comm )
+  CALL mp_bcast ( ngk_g, ionode_id, world_comm )
 
   nkbl = nkstot / kunit
   nkl = kunit * ( nkbl / npool )
@@ -502,20 +514,15 @@
   ike = iks + nkl - 1
 
   npw_g = 0
-  ALLOCATE ( itmp ( npwx ) )
+  IF ( nks > 1 ) REWIND ( iunigk )
   DO ik = 1, nks
-    DO ig = 1, npwx
-      itmp ( ig ) = 0
-    ENDDO
-    npw = npwx
-    CALL gk_sort ( xk ( 1, ik + iks - 1 ), ngm, g, ecutwfc / tpiba2, &
-      npw, itmp ( 1 ), g2kin )
+    IF ( nks > 1 ) READ ( iunigk ) igk
+    npw = ngk ( ik )
     DO ig = 1, npw
-      igk_l2g = ig_l2g ( itmp ( ig ) )
+      igk_l2g = ig_l2g ( igk ( ig ) )
       IF ( igk_l2g .GT. npw_g ) npw_g = igk_l2g
     ENDDO
   ENDDO
-  DEALLOCATE ( itmp )
   CALL mp_max ( npw_g, world_comm )
 
   CALL create_directory ( output_dir_name )
@@ -546,13 +553,13 @@
 
     IF ( ionode ) THEN
       CALL iotk_open_write ( iu, FILE = TRIM ( filename ), ROOT="GK-VECTORS", BINARY = .TRUE. )
-      CALL iotk_write_dat ( iu, "NUMBER_OF_GK-VECTORS", ngk ( ik ) )
+      CALL iotk_write_dat ( iu, "NUMBER_OF_GK-VECTORS", ngk_g ( ik ) )
       CALL iotk_write_dat ( iu, "MAX_NUMBER_OF_GK-VECTORS", ngkmax )
       CALL iotk_write_dat ( iu, "GAMMA_ONLY", .FALSE. )
       CALL iotk_write_attr ( attr, "UNITS", "2 pi / a", FIRST = .TRUE. )
       CALL iotk_write_dat ( iu, "K-POINT_COORDS", k ( :, ik ), ATTR = attr )
     ENDIF
-#ifdef __PARA
+#ifdef __MPI
     CALL mp_barrier ( world_comm )
     CALL MPI_Gather ( igk_dist ( :, ik ) , ngkdist_l, MPI_INTEGER, &
     igk_buf, ngkdist_l, MPI_INTEGER, &
@@ -564,8 +571,8 @@
     ENDDO
 #endif
     IF ( ionode ) THEN
-      CALL iotk_write_dat ( iu, "INDEX", igk_buf ( 1 : ngk ( ik ) ) )
-      CALL iotk_write_dat ( iu, "GRID", gvec ( 1 : 3, igk_buf ( 1 : ngk ( ik ) ) ), COLUMNS = 3 )
+      CALL iotk_write_dat ( iu, "INDEX", igk_buf ( 1 : ngk_g ( ik ) ) )
+      CALL iotk_write_dat ( iu, "GRID", gvec ( 1 : 3, igk_buf ( 1 : ngk_g ( ik ) ) ), COLUMNS = 3 )
       CALL iotk_close_write ( iu )
     ENDIF
 
@@ -599,7 +606,7 @@
       IF ( ionode ) THEN
         CALL iotk_open_write ( iu, FILE = TRIM ( filename ), ROOT = "WFC", BINARY = .TRUE. )
         CALL iotk_write_attr ( attr, "ngw", npw_g, FIRST = .TRUE. )
-        CALL iotk_write_attr ( attr, "igwx", ngk ( ik ) )
+        CALL iotk_write_attr ( attr, "igwx", ngk_g ( ik ) )
         CALL iotk_write_attr ( attr, "gamma_only", .FALSE. )
         CALL iotk_write_attr ( attr, "nbnd", nb )
         CALL iotk_write_attr ( attr, "ik", ik )
@@ -610,7 +617,7 @@
         CALL iotk_write_empty ( iu, "INFO", attr )
       ENDIF
       DO ib = 1, nb
-#ifdef __PARA
+#ifdef __MPI
         CALL mp_barrier ( world_comm )
         CALL MPI_Gather ( wfng_dist ( :, ib, is, ik ), ngkdist_l, MPI_DOUBLE_COMPLEX, &
         wfng_buf ( :, is ), ngkdist_l, MPI_DOUBLE_COMPLEX, &
@@ -621,14 +628,14 @@
           wfng_buf ( ig, is ) = wfng_dist ( ig, ib, is, ik )
         ENDDO
 #endif
-        IF ( ionode ) CALL iotk_write_dat ( iu, "evc" // iotk_index ( ib ), wfng_buf ( 1 : ngk ( ik ), is ) )
+        IF ( ionode ) CALL iotk_write_dat ( iu, "evc" // iotk_index ( ib ), wfng_buf ( 1 : ngk_g ( ik ), is ) )
       ENDDO
       IF ( ionode ) CALL iotk_close_write ( iu )
 
     ENDDO
   ENDDO
 
-  DEALLOCATE ( ngk )
+  DEALLOCATE ( ngk_g )
   DEALLOCATE ( k )
   DEALLOCATE ( en )
   DEALLOCATE ( oc )
diff -ur espresso-5.1_pristine/PP/src/pw2bgw.f90 espresso-5.1/PP/src/pw2bgw.f90
--- espresso-5.1_pristine/PP/src/pw2bgw.f90	2014-05-28 07:43:07.000000000 -0700
+++ espresso-5.1/PP/src/pw2bgw.f90	2014-10-10 12:16:09.791265000 -0700
@@ -164,9 +164,10 @@
   character (len=256), external :: trimcheck
   character (len=1), external :: lowercase
 
-#ifdef __PARA
-  CALL mp_startup ( world_comm )
+#ifdef __MPI
+  CALL mp_startup ( )
 #endif
+
   CALL environment_start ( codename )
 
   prefix = 'prefix'
@@ -280,6 +281,16 @@
   if (okpaw) call errore ( 'pw2bgw', 'BGW cannot use PAW.', 4 )
   if (gamma_only) call errore ( 'pw2bgw', 'BGW cannot use gamma-only run.', 5 )
   if (nspin == 4) call errore ( 'pw2bgw', 'BGW cannot use spinors.', 6 )
+  if (real_or_complex == 1 .AND. vxc_flag .AND. vxc_offdiag_nmax > 0) &
+    call errore ( 'pw2bgw', 'Off-diagonal matrix elements of Vxc ' // &
+    'with real wavefunctions are not implemented, compute them in ' // &
+    'Sigma using VXC.', 7)
+
+  ! this is needed to compute igk and store in iunigk
+  ! cannot use gk_sort because for some k-points
+  ! gk_sort generates different igk on every call
+  CALL openfil ( )
+  CALL hinit0 ( )
 
   CALL openfil_pp ( )
 
@@ -388,8 +399,12 @@
   ENDIF
 
   CALL environment_end ( codename )
+
   CALL stop_pp ( )
 
+  ! this is needed because openfil is called above
+  CALL close_files ( .false. )
+
   STOP
 
 CONTAINS
@@ -404,7 +419,7 @@
   USE constants, ONLY : pi, tpi, eps6
   USE fft_base, ONLY : dfftp
   USE gvect, ONLY : ngm, ngm_g, ig_l2g, g, mill, ecutrho
-  USE io_files, ONLY : iunwfc, nwordwfc
+  USE io_files, ONLY : iunwfc, nwordwfc, iunigk
   USE io_global, ONLY : ionode, ionode_id
   USE ions_base, ONLY : nat, atm, ityp, tau
   USE kinds, ONLY : DP
@@ -418,8 +433,8 @@
   USE start_k, ONLY : nk1, nk2, nk3, k1, k2, k3
   USE symm_base, ONLY : s, ftau, nsym
   USE wavefunctions_module, ONLY : evc
-  USE wvfct, ONLY : npwx, nbnd, npw, et, wg, g2kin, ecutwfc
-#ifdef __PARA
+  USE wvfct, ONLY : npwx, nbnd, npw, et, wg, g2kin, ecutwfc, igk
+#ifdef __MPI
   USE parallel_include, ONLY : MPI_DOUBLE_COMPLEX
 #endif
 
@@ -622,7 +637,7 @@
       et_g ( ib, ik ) = et ( ib, ik )
     ENDDO
   ENDDO
-#ifdef __PARA
+#ifdef __MPI
   CALL poolrecover ( et_g, nb, nk_g, nk_l )
   CALL mp_bcast ( et_g, ionode_id, world_comm )
 #endif
@@ -653,13 +668,14 @@
 
     DO ik = 1, nk_l
       DO ib = 1, nb
-        wg_g ( ib, ik ) = wg ( ib, ik ) 
-        IF ( abs ( wk ( ik ) ) .GT. eps6 ) THEN
-          wg_g ( ib, ik ) = wg_g ( ib, ik ) / wk ( ik ) 
+        IF ( wk(ik) == 0.D0 ) THEN
+          wg_g(ib,ik) = wg(ib,ik)
+        ELSE
+          wg_g(ib,ik) = wg(ib,ik) / wk(ik)
         ENDIF
       ENDDO
     ENDDO
-#ifdef __PARA
+#ifdef __MPI
     CALL poolrecover ( wg_g, nb, nk_g, nk_l )
 #endif
     DO ik = 1, nk_g
@@ -695,23 +711,17 @@
 
   ALLOCATE ( igk_l2g ( npwx, nk_l ) )
 
-  ALLOCATE ( itmp ( npwx ) )
+  IF ( nk_l > 1 ) REWIND ( iunigk )
   DO ik = 1, nk_l
-    DO i = 1, npwx
-      itmp ( i ) = 0
-    ENDDO
-    npw = npwx
-    CALL gk_sort ( xk ( 1, ik + iks - 1 ), ng_l, g, ecutwfc / tpiba2, &
-      npw, itmp ( 1 ), g2kin )
+    IF ( nk_l > 1 ) READ ( iunigk ) igk
+    npw = ngk ( ik )
     DO ig = 1, npw
-      igk_l2g ( ig, ik ) = ig_l2g ( itmp ( ig ) )
+      igk_l2g ( ig, ik ) = ig_l2g ( igk ( ig ) )
     ENDDO
     DO ig = npw + 1, npwx
       igk_l2g ( ig, ik ) = 0
     ENDDO
-    ngk ( ik ) = npw
   ENDDO
-  DEALLOCATE ( itmp )
 
   ALLOCATE ( ngk_g ( nk_g ) )
 
@@ -904,7 +914,7 @@
         DO ig = igwx + 1, ngkdist_g
           wfng_buf ( ig, is ) = ( 0.0D0, 0.0D0 )
         ENDDO
-#ifdef __PARA
+#ifdef __MPI
         CALL mp_barrier ( world_comm )
         CALL MPI_Scatter ( wfng_buf ( :, is ), ngkdist_l, MPI_DOUBLE_COMPLEX, &
         wfng_dist ( :, ib, is ), ngkdist_l, MPI_DOUBLE_COMPLEX, &
@@ -937,7 +947,7 @@
       ENDIF
       DO ib = 1, nb
         DO is = 1, ns
-#ifdef __PARA
+#ifdef __MPI
           CALL mp_barrier ( world_comm )
           CALL MPI_Gather ( wfng_dist ( :, ib, is ), ngkdist_l, &
           MPI_DOUBLE_COMPLEX, wfng_buf ( :, is ), ngkdist_l, &
@@ -1424,8 +1434,8 @@
   USE fft_base, ONLY : dfftp
   USE fft_interfaces, ONLY : fwfft, invfft
   USE gvect, ONLY : ngm, g, nl
-  USE io_files, ONLY : nwordwfc, iunwfc
-  USE klist, ONLY : xk, nkstot
+  USE io_files, ONLY : nwordwfc, iunwfc, iunigk
+  USE klist, ONLY : xk, nkstot, ngk, nks
   USE lsda_mod, ONLY : nspin, isk
   USE mp, ONLY : mp_sum
   USE mp_world, ONLY : world_comm
@@ -1456,10 +1466,11 @@
   rho%of_r (:, :) = 0.0D0
 
   ! take psi to R-space, compute rho in R-space
+  IF ( nks > 1 ) REWIND ( iunigk )
   DO ik = iks, ike
     is = isk (ik)
-    CALL gk_sort (xk (1, ik - iks + 1), ngm, g, ecutwfc &
-      / tpiba2, npw, igk, g2kin)
+    IF ( nks > 1 ) READ ( iunigk ) igk
+    npw = ngk ( ik - iks + 1 )
     CALL davcio (evc, 2*nwordwfc, iunwfc, ik - iks + 1, -1)
     DO ib = rhog_nvmin, rhog_nvmax
       psic (:) = (0.0D0, 0.0D0)
@@ -1806,9 +1817,9 @@
   USE fft_base, ONLY : dfftp
   USE fft_interfaces, ONLY : invfft
   USE gvect, ONLY : ngm, g, nl
-  USE io_files, ONLY : nwordwfc, iunwfc
+  USE io_files, ONLY : nwordwfc, iunwfc, iunigk
   USE io_global, ONLY : ionode
-  USE klist, ONLY : xk, nkstot
+  USE klist, ONLY : xk, nkstot, nks, ngk
   USE lsda_mod, ONLY : nspin, isk
   USE mp, ONLY : mp_sum
   USE mp_pools, ONLY : kunit, my_pool_id, intra_pool_comm, &
@@ -1886,9 +1897,10 @@
   ENDIF
   CALL v_xc (rho, rho_core, rhog_core, etxc, vtxc, vxcr)
 
+  IF ( nks > 1 ) REWIND ( iunigk )
   DO ik = iks, ike
-    CALL gk_sort (xk (1, ik - iks + 1), ngm, g, ecutwfc &
-      / tpiba2, npw, igk, g2kin)
+    IF ( nks > 1 ) READ ( iunigk ) igk
+    npw = ngk ( ik - iks + 1 )
     CALL davcio (evc, 2*nwordwfc, iunwfc, ik - iks + 1, -1)
     IF (ndiag .GT. 0) THEN
       DO ib = diag_nmin, diag_nmax
@@ -1997,10 +2009,10 @@
   USE fft_interfaces, ONLY : fwfft, invfft
   USE funct, ONLY : exx_is_active
   USE gvect, ONLY : ngm, g, nl
-  USE io_files, ONLY : nwordwfc, iunwfc
+  USE io_files, ONLY : nwordwfc, iunwfc, iunigk
   USE io_global, ONLY : ionode
   USE kinds, ONLY : DP
-  USE klist, ONLY : xk, nkstot
+  USE klist, ONLY : xk, nkstot, nks, ngk
   USE lsda_mod, ONLY : nspin, isk
   USE mp, ONLY : mp_sum
   USE mp_pools, ONLY : kunit, my_pool_id, intra_pool_comm, &
@@ -2079,9 +2091,10 @@
   ENDIF
   CALL v_xc (rho, rho_core, rhog_core, etxc, vtxc, vxcr)
 
+  IF ( nks > 1 ) REWIND ( iunigk )
   DO ik = iks, ike
-    CALL gk_sort (xk (1, ik - iks + 1), ngm, g, ecutwfc &
-      / tpiba2, npw, igk, g2kin)
+    IF ( nks > 1 ) READ ( iunigk ) igk
+    npw = ngk ( ik - iks + 1 )
     CALL davcio (evc, 2*nwordwfc, iunwfc, ik - iks + 1, -1)
     IF (ndiag .GT. 0) THEN
       DO ib = diag_nmin, diag_nmax
@@ -2418,6 +2431,7 @@
   USE constants, ONLY : pi, tpi, eps6
   USE fft_base, ONLY : dfftp
   USE gvect, ONLY : ngm, ngm_g, ig_l2g, g, mill, ecutrho
+  USE io_files, ONLY : iunigk
   USE io_global, ONLY : ionode, ionode_id
   USE ions_base, ONLY : nat, atm, ityp, tau, nsp
   USE kinds, ONLY : DP
@@ -2432,7 +2446,7 @@
   USE symm_base, ONLY : s, ftau, nsym
   USE uspp, ONLY : nkb, vkb, deeq
   USE uspp_param, ONLY : nhm, nh
-  USE wvfct, ONLY : npwx, npw, g2kin, ecutwfc
+  USE wvfct, ONLY : npwx, npw, g2kin, ecutwfc, igk
 
   IMPLICIT NONE
 
@@ -2598,18 +2612,14 @@
   ALLOCATE ( igk_l2g ( npwx, nks ) )
   ngk_g = 0
   igk_l2g = 0
-  ALLOCATE ( itmp ( npwx ) )
+  IF ( nks > 1 ) REWIND ( iunigk )
   DO ik = 1, nks
-    itmp = 0
-    npw = npwx
-    CALL gk_sort ( xk ( 1, ik + iks - 1 ), ngm, g, ecutwfc / tpiba2, &
-      npw, itmp ( 1 ), g2kin )
+    IF ( nks > 1 ) READ ( iunigk ) igk
+    npw = ngk ( ik )
     DO ig = 1, npw
-      igk_l2g ( ig, ik ) = ig_l2g ( itmp ( ig ) )
+      igk_l2g ( ig, ik ) = ig_l2g ( igk ( ig ) )
     ENDDO
-    ngk ( ik ) = npw
   ENDDO
-  DEALLOCATE ( itmp )
   DO ik = 1, nks
     ngk_g ( ik + iks - 1 ) = ngk ( ik )
   ENDDO
@@ -2656,6 +2666,7 @@
 
   ALLOCATE ( igwk ( npwx_g ) )
 
+  IF ( nks > 1 ) REWIND ( iunigk )
   DO i = 1, nkstot
 
     ik = kmap ( i )
@@ -2691,13 +2702,10 @@
 
     local_pw = 0
     IF ( ik .GE. iks .AND. ik .LE. ike ) THEN
-      ALLOCATE ( itmp ( npwx ) )
-      npw = npwx
-      CALL gk_sort ( xk ( 1, ik ), ngm, g, ecutwfc / tpiba2, &
-        npw, itmp ( 1 ), g2kin )
-      CALL init_us_2 ( npw, itmp, xk ( 1, ik ), vkb )
-      local_pw = ngk ( ik - iks + 1 )
-      DEALLOCATE ( itmp )
+      IF ( nks > 1 ) READ ( iunigk ) igk
+      npw = ngk ( ik - iks + 1 )
+      CALL init_us_2 ( npw, igk, xk ( 1, ik ), vkb )
+      local_pw = npw
     ENDIF
 
     ALLOCATE ( igwf_l2g ( local_pw ) )
@@ -2761,7 +2769,7 @@
 
       IF ( ionode ) THEN
         WRITE ( unit ) nrecord
-        WRITE ( unit ) igwx
+        WRITE ( unit ) ngk_g ( ik )
         WRITE ( unit ) ( vkb_g ( ig ), ig = 1, igwx )
       ENDIF
 
