Improved code flow and documentation