clean up code; remove some duplication