Added tests and fixes for invalid pointers.

Also made the error handling of the file reading much better.