rearranging lmsvd.m to make it easier to code and understand