I managed to fix it by adding a reading instruction before any of the while cycles, it seems the serial holds a character when the loop restarts for some reason, I'll address it later once everything is working and I can concentrate on performance. For now it's doing what I need. Thanks everyone!