Trap memory leak and handle error. Close #42