change clsoe to end
reverting end on error