clean up of functions and added more commenting