fixes and code refactoring