made some changes and added comments