diff --git a/newbrt/pma-test.c b/newbrt/pma-test.c
index 58f99c351edb2f3aa59d93b946605c237221253b..f8bd0caab45148a07b2edc1dd10a11a1e65ba5dc 100644
--- a/newbrt/pma-test.c
+++ b/newbrt/pma-test.c
@@ -1433,12 +1433,110 @@ void test_pma_double_delete() {
     assert(error == 0);
 }
 
+void test_pma_cursor_first_delete_last() {
+    printf("test_pma_cursor_first_delete_last\n");
+
+    int error;
+    PMA pma;
+
+    error = pma_create(&pma, default_compare_fun);
+    assert(error == 0);
+
+    DBT key, val;
+    int k, v;
+
+    int i;
+    for (i=1; i<=2; i++) {
+        k = htonl(i);
+        v = i;
+        fill_dbt(&key, &k, sizeof k);
+        fill_dbt(&val, &v, sizeof v);
+        error = pma_insert(pma, &key, &val, 0);
+        assert(error == 0);
+    }
+    assert(pma_n_entries(pma) == 2);
+
+    PMA_CURSOR pmacursor;
+
+    error = pma_cursor(pma, &pmacursor);
+    assert(error == 0);
+
+    error = pma_cursor_set_position_first(pmacursor);
+    assert(error == 0);
+
+    k = htonl(1);
+    fill_dbt(&key, &k, sizeof k);
+    error = pma_delete(pma, &key, 0);
+    assert(error == 0);
+    assert(pma_n_entries(pma) == 2);
+
+    error = pma_cursor_set_position_last(pmacursor);
+    assert(error == 0);
+    assert(pma_n_entries(pma) == 1);
+
+    error = pma_cursor_free(&pmacursor);
+    assert(error == 0);
+
+    error = pma_free(&pma);
+    assert(error == 0);
+}
+
+void test_pma_cursor_last_delete_first() {
+    printf("test_pma_cursor_last_delete_first\n");
+
+    int error;
+    PMA pma;
+
+    error = pma_create(&pma, default_compare_fun);
+    assert(error == 0);
+
+    DBT key, val;
+    int k, v;
+
+    int i;
+    for (i=1; i<=2; i++) {
+        k = htonl(i);
+        v = i;
+        fill_dbt(&key, &k, sizeof k);
+        fill_dbt(&val, &v, sizeof v);
+        error = pma_insert(pma, &key, &val, 0);
+        assert(error == 0);
+    }
+    assert(pma_n_entries(pma) == 2);
+
+    PMA_CURSOR pmacursor;
+
+    error = pma_cursor(pma, &pmacursor);
+    assert(error == 0);
+
+    error = pma_cursor_set_position_last(pmacursor);
+    assert(error == 0);
+
+    k = htonl(2);
+    fill_dbt(&key, &k, sizeof k);
+    error = pma_delete(pma, &key, 0);
+    assert(error == 0);
+    assert(pma_n_entries(pma) == 2);
+
+    error = pma_cursor_set_position_first(pmacursor);
+    assert(error == 0);
+    assert(pma_n_entries(pma) == 1);
+
+    error = pma_cursor_free(&pmacursor);
+    assert(error == 0);
+
+    error = pma_free(&pma);
+    assert(error == 0);
+}
+
 void test_pma_delete() {
     test_pma_delete_shrink(256);  memory_check_all_free();
     test_pma_delete_random(256);  memory_check_all_free();
     test_pma_delete_cursor(32);   memory_check_all_free();
     test_pma_delete_insert();     memory_check_all_free();
     test_pma_double_delete();     memory_check_all_free();
+    test_pma_cursor_first_delete_last(); memory_check_all_free();
+    test_pma_cursor_last_delete_first(); memory_check_all_free();
 }
 
 void test_pma_already_there() {
diff --git a/newbrt/pma.c b/newbrt/pma.c
index aea3cb4c9cd19445c7e6826fcc5cd51fc3c383f9..590d703e1121993444f9c30ab460a4ebbcf546af 100644
--- a/newbrt/pma.c
+++ b/newbrt/pma.c
@@ -376,12 +376,21 @@ int pma_cursor_get_pma(PMA_CURSOR c, PMA *pmap) {
 
 int pma_cursor_set_position_last (PMA_CURSOR c) {
     PMA pma = c->pma;
+    int result = 0;
+    int old_position = c->position;
     c->position=pma->N-1;
     while (!kv_pair_valid(c->pma->pairs[c->position])) {
-	if (c->position>0) c->position--;
-	else return DB_NOTFOUND;
+	if (c->position>0) 
+            c->position--;
+	else {
+            c->position = -1;
+            result = DB_NOTFOUND;
+            break;
+        } 
     }
-    return 0;
+    if (old_position != c->position)
+        __pma_delete_resume(pma, old_position);
+    return result;
 }
 
 int pma_cursor_set_position_prev (PMA_CURSOR c) {
@@ -401,12 +410,21 @@ int pma_cursor_set_position_prev (PMA_CURSOR c) {
 
 int pma_cursor_set_position_first (PMA_CURSOR c) {
     PMA pma = c->pma;
+    int result = 0;
+    int old_position = c->position;
     c->position=0;
     while (!kv_pair_valid(c->pma->pairs[c->position])) {
-	if (c->position+1<pma->N) c->position++;
-	else return DB_NOTFOUND;
+	if (c->position+1<pma->N) 
+            c->position++;
+	else {
+            c->position = -1;
+            result =DB_NOTFOUND;
+            break;
+        }
     }
-    return 0;
+    if (old_position != c->position)
+        __pma_delete_resume(pma, old_position);
+    return result;
 }
     
 int pma_cursor_set_position_next (PMA_CURSOR c) {