Fix Bugs and add Tests