Refactoring and little improvement of performance