Refactor and code formatting