reduce duplicate code and fix messages