reorganizes code and simplifies some comments