Sync with Edward's changes. Code cleanup.