Can this function be improved performance wise? - Theo's Typesafe Cult