cleanup; remove unecessary variables; re-order some code