A big refactor, structure and efficiency improvement

A big refactor, structure and efficiency improvement, better tests