Final refactor of code