Error checking and logging

Error checking and logging