updated to include logging function and exception handling