Updated code, added comments