random improvements in clarity and a few small refactorings