Does anyone have any suggestions to potentially improve and make this function work better? - C#