added missing functions and better structure, not finished