Refactor and style code