Rewrote Javascript as c#