Many changes and bugs fixed