Major update, reworked code, added LM specific portions of code.