bug fixes; added some optimization; added more documentation