added comments for all functions and reorganized functions