Refactor to do it properly as I no longer understand what it does...