Cleaned up some stuff and added error logging