fixed some bugs introduced with last changes + tests