Improving logic and adding documentation.