initial code
Port to C#