refactored c# code