Fix compile errors and code cleanup