Refactored to make things more extendable.  No new functionality was added.