Ported to numpy and moved helpers to baseline for clarity.