rewritten to mirror apache check code style (for better maintainability)