Corrected errors in code and added comments