Adds logging and some additional error checking.