Added documentation and improved code structure