Tidied and commented the new cleanup code.