Refactored and added documentation.