now the result is returned, also added write error check