code refactoring, clean-up and other improvements