Catch more errors and fix a bug