added missing pieces fixing some bugs