Refactoring for more uniform code style, better comments.