fixed indentation and reduced code duplication