avoiding those ugly casts

could be more dry by factoring out collect etc etc in a method, but felt the readability is better this way