Refactored to use functions and hashes.