How to optimize this? - C#