adding decoding, cleaning up memory management etc.