fixed cmake bug and some minor refactoring.