tidied up RAM code