Several enhancements, refactoring,and bug fixes