shortened code using reflection (by james)