factorize some code, add a test, modify the makekfile