added functions to free handle
fix error codes