Simplified, added docs, and refactored.