integrate hfang's code, currently not working
resolve merge conflict