fixing with statement throwing exceptions