Based on the code originally written by Qiuhan Ding Change-Id: Ib66e24f49d0b6fb2ae21ea1fca7b9ec62ecb753a Refs: #3289, #1872