more code cleanups and lots of comments