this entire code here looks ugly imho but I didn't come up with a nicer solution. then again I wonder if it would not be more readable by simply duplicating the code