Clean Memory code; add documentation