Fixed memory problems and added comments.