Added comments, checked mallocs for NULL