Added commenting, improved efficiency