Improved code structure, better commented and explained