Cleaned the code and fixed #2894 while at it ...