added handling errors and logging