improved error throwing code