added test, modify and clean core code.