updated code and added comments