Fixing some bugs and tests