Bunch of fixes and little bit optimization.