Fixed tricky reference problem.

Also added a get_error method to get last error