Working source code.  Simplified.