Fix some compilation errors and clean up codes