Refactors and creates index