code refactoring; some improvments