diff --git a/libhts/libhts.py b/libhts/libhts.py
index 5932deb011607f62c73aec96404c71c8880cb11c..07b5698f9d0dd399e8b270b75596b74a21b20322 100644
--- a/libhts/libhts.py
+++ b/libhts/libhts.py
@@ -239,7 +239,7 @@ bowtie2_function_selector = {
 def make_seeding_function(seeding_string):
     """Generates a function that computes the seeding pattern given a
     string representing bowtie2 seeding settings (-L and -i options).
-    
+
     >>> make_seeding_function("-L 6 -i S,1,0.8")(18)
     [[0, 6], [4, 10], [8, 14], [12, 18]]
     """
diff --git a/scripts/sam2indexedbam.sh b/scripts/sam2indexedbam.sh
index f0a37c3b62d9e3518410b135fb7e351174e8710d..a530f7db106557594fe779439620cab2f35f6fc1 100755
--- a/scripts/sam2indexedbam.sh
+++ b/scripts/sam2indexedbam.sh
@@ -52,7 +52,6 @@ else
     filter="-F 4"
 fi
 
-# Test environment variable THREADS
 if [[ ${SAMTOOLS_THREADS} ]]
 then
     threads_option="--threads ${SAMTOOLS_THREADS}"
@@ -60,17 +59,27 @@ else
     threads_option=""
 fi
 
+if [[ ${SAMTOOLS_MEM} ]]
+then
+    mem_option="-m ${SAMTOOLS_MEM}"
+else
+    mem_option=""
+fi
+
 if [ -e ${SAM} ]
 then
     # -u: output uncompressed bam, to save time when piping to samtools sort
-    samtools view ${threads_option} -u ${filter} ${SAM} | niceload --mem 500M samtools sort ${threads_option} -T ${tmpdir} -o ${SAM%.${ext}}_sorted.bam - || error_exit "samtools sort failed"
+    #samtools view ${threads_option} -u ${filter} ${SAM} | niceload --mem 500M samtools sort ${mem_option} ${threads_option} -T ${tmpdir} -o ${SAM%.${ext}}_sorted.bam - || error_exit "samtools sort failed"
+    samtools view ${threads_option} -u ${filter} ${SAM} | samtools sort ${mem_option} ${threads_option} -T ${tmpdir} -o ${SAM%.${ext}}_sorted.bam - || error_exit "samtools sort failed"
     #cat ${SAM} | samtools view ${threads_option} -u ${filter} - | samtools sort ${threads_option} -o ${SAM%.${ext}}_sorted.bam - || error_exit "samtools sort failed"
 elif [ -e ${SAM}.gz ]
 then
-    zcat ${SAM}.gz | samtools view ${threads_option} -u ${filter} - | niceload --mem 500M samtools sort ${threads_option} -T ${tmpdir}  -o ${SAM%.${ext}}_sorted.bam - || error_exit "samtools sort failed"
+    #zcat ${SAM}.gz | samtools view ${threads_option} -u ${filter} - | niceload --mem 500M samtools sort ${mem_option} ${threads_option} -T ${tmpdir}  -o ${SAM%.${ext}}_sorted.bam - || error_exit "samtools sort failed"
+    zcat ${SAM}.gz | samtools view ${threads_option} -u ${filter} - | samtools sort ${mem_option} ${threads_option} -T ${tmpdir}  -o ${SAM%.${ext}}_sorted.bam - || error_exit "samtools sort failed"
 elif [ -e ${SAM}.bz2 ]
 then
-    bzcat ${SAM}.bz2 | samtools view ${threads_option} -u ${filter} - | niceload --mem 500M samtools sort ${threads_option} -T ${tmpdir}  -o ${SAM%.${ext}}_sorted.bam - || error_exit "samtools sort failed"
+    #bzcat ${SAM}.bz2 | samtools view ${threads_option} -u ${filter} - | niceload --mem 500M samtools sort ${mem_option} ${threads_option} -T ${tmpdir}  -o ${SAM%.${ext}}_sorted.bam - || error_exit "samtools sort failed"
+    bzcat ${SAM}.bz2 | samtools view ${threads_option} -u ${filter} - | samtools sort ${mem_option} ${threads_option} -T ${tmpdir}  -o ${SAM%.${ext}}_sorted.bam - || error_exit "samtools sort failed"
 else
     echo "${SAM} not found, neither .gz or .bz2 versions."
     echo "Aborting."