Some logic and style revisions