Bug fixes and refactoring
Bug fix