No offense, but this part of code looks very confusing.

Is there no better way to do this?