Extract code out of main

Signed-off-by: Jonathan Barnes <j.barnes@pivotallabs.com>