adds some cleanup code inspired by and from Roots mostly