Convert to C#, refactor