Refactor code and update style