moved most of this code to lampymath.