Adds exception handling, adds debug messages