added more comments and logging