More code cleanup and a fix or two

Signed-off-by: ziycon <ziycon.eire@gmail.com>