Applied first portion of fixes, proposed by Vicente J. Botet Escriba
Applied one more part of changes proposed by Vicente J. Botet Escriba