Adding unit (integration) tests and fixes