refactored the index method