Added exception handling and logging