This seems ugly but I can't think of a better solution.