Add comments and reorganize function order