Much more commenting, more security and error checking