Changed the code to sort of oop and it seems to work so far