Fixes for DB-2607 (getLastId) and DB-2657 (memory leaks)