Move core to library and add tests