Various cleanup, fix warnings and add some comments.