Thanks to Natanael Copa <ncopa@users.sourceforge.net> for patches.
