Add Unit tests and fix some logics and templates