improved functions and add comments