Reorganize the code, add comments