move to top level function and add tests

Signed-off-by: Stephen Gran <stephen.gran@piksel.com>