code review

reviewed code, fixed some details on handling memory and variables