Tried to fix serveral bugs.