Fixed most of the bugs. There maybe a problem with freeing a pointer in
the implementation file.