some code refactoring. Added new features.