Added comments to explain all code ready for integration with the main system.