Extract test and some cleanup

I cleaned up the extract function a little bit and added some basic tests for it.