bug fixes, enhancements and refactoring