cleanup and a few bugfix.