Performed code cleanup and added comprehensive comments