I modified some code for porting and compatibility.
