I'm not very happy with this, but couldn't find another way of doing it.