dvolgyes' fixes

I made some bug fixes and few performance fixes. Please, review them.