Code refactor and cleaning