Working traingdm function