Code cleanup, fix code errors