added error checking and better location handling