Some code re-arranging and improved documentation.