anchor.diff:  		{aborted, Reason} ->
   			jsx:encode([{<<"error">>, Reason}])
   	end.
  -	
  +
  +%% Save news
   save_to_db(Req) ->
   	Content = get_content(Req),
  -	case validate_html(Content) of
  -		true->
  +	case validate(Content) of
  +		{fail, Error} ->
  +			Error;
  +		_ ->
   			Escaped = escape_html_chars(Content),
   			Result = mnesia_news:create_news(Escaped),
  -			result_to_json(Result);
  -		false-> 
  -			jsx:encode([{<<"error">>, <<"Not valid html.">>}])
  +			result_to_json(Result)
   	end.
   
  +%% Update news
   update_db(Req) ->
  -	Id = get_id(Req),
  -	case Id of
  -		0 ->
  -			<<"{\"error\": \"Empty news_id path variable.\"}">>;
  -		_ ->
  -			Content = get_content(Req),	
  -			case validate_html(Content) of
  -				true->
  +	case Id = get_id(Req) of
  +		{fail, Error} ->
  +			Error;
  +		_ when Id > 0 ->
  +			Content = get_content(Req),
  +			case validate(Content) of
  +				{fail, Error} ->
  +					Error;
  +				_ ->
   					Escaped = escape_html_chars(Content),
   					Result = mnesia_news:update_news(Id, Escaped),
  -					result_to_json(Result);
  -				false-> 
  -					jsx:encode([{<<"error">>, <<"Not valid html.">>}])
  -			end
  +					result_to_json(Result)
  +			end;
  +		_ -> 
  +			<<"{\"error\": \"Empty news_id path variable.\"}">>
  +	end.
  +
  +%% Validate html
  +validate(Content) ->
  +	case validate_html(Content) of
  +		false -> 
  +			{fail, jsx:encode([{<<"error">>, <<"Not valid html.">>}])};
  +		true -> true
   	end.
   
   %% Convert responce to json
  
positive.code: 	Content.
  
  get_id(Req) ->
  	Number = cowboy_req:binding(paste_id, Req, 0),
  	try erlang:binary_to_integer(Number) 
  	catch
  	   error:badarg -> 0
  	end.
  	
  %% Convert db data for rest representation
  
negative.code: %% Internal functions
  %% ====================================================================
  
  %% Get news
  get_from_db(Req) ->
  	Id = get_id(Req),
  	case  Id of
  		{fail, Error} ->
  			 Error;
  		_ when Id =< 0 ->
  			 Resp = mnesia_news:get_all_news(),				
  			 convert_response(Resp);
  		_ ->
  			 Resp = mnesia_news:get_news(Id),
  			 convert_response(Resp)
  	end.
  
  convert_response(Resp)->
  	case Resp of
  		{atomic, Result} ->
  			Responce = lists:reverse(convert_news(Result, [])),			
  