Bug fixes. Code optimization.

Bug fixes. Code optimization.