Slight code restructure and added code comments