Complete refactor of code