back port fixes from real implementation