initial s2w and dt2w code import from @showp1984