edited to allow for potentially faster parallelization