major improvements and code refactoring