Cleaned up logging and added comments.