finished solution to lcsm, could stand to be faster