TRANSLATED JS script into CSharp.