Extracted redundant code to utils.
Use more explicit variable names, code cleanup.
Simplified detect API output.