implmentation of KMP