Changes after small testing, still needs much improvements
First version that seems to be working as expected