modified to work with errors