code clean up

house keeping, cleaned code and commented code
more code cleaning and documentation

code cleanup and documentation of some functions