Several fixes

Original author: Igor Paliychuk