split out common code into library

Signed-off-by: Andreas Müller <schnitzeltony@googlemail.com>