cleaned up version for parallelization