Refactored for niceness and documentation.