added freeing functions and an error code to header
added error code for dealing failure