The not prettiest code ... I wonder if there is a better way