added unit tests + some code cleanup