Some refactoring and commenting.