Refactor code to conform to indexed registers and optimise