Remove duplicate code and fix indentation