Optimize the code and fixed the bug

Signed-off-by: jssoscar