Updated error checking and fixed some minor bugs.