Some refactoring of code and further improvements.