make some fixes and add unitests