merged several compatibility fixes [Thanks to Carlos Ungil]