added error checking and caches