edited exists function and corrected error checking