cleaned code and added some comments.