Commented and re-factored code