Code cleanup and bugfix.