cleaned the code and added comments.