Refactoring and added a performance test