Fixed some major cockups and some refactor