Remove trailing lines and add newline at end of file