Adding tests + some implementation changes.