Added optional error field to helper function