fixed logic and syntax errors, added comments