Several fixes to LMA (ported from Java)