Fix for 0014031 and some refactoring