Removed debugging code; removed superfluous functions; added legend;