Fix and improve RA-handling code