Importing Junjie's code. Some cleanup performed. Still more work to do.