Added some logging and improvements.