Copied from Apple.com, add some debug lines