More elaborate team changing and some code cleanup.