Add copyright headers, some misc code cleanup