logic deduplication and coding style fixes