added proper logging and exception handling