added doc to the code and cleaned the code