fixed many bugs and improved the output to console.