Refactor and Add tests in general

This PR consists of several commits adding some test cases and refactoring on several new features.

Not done completely, but I share them in advance for your reference.