fix some minor issues. prepare loging. optimize code a bit.