If it's an assignment, then I highly recommend to ask your teacher/assistant as they are being paid to help you, plus they know exactly what you should be doing.
With the given code and the very generic description "lagging" we can't really help you further.
Create a minimal and compilable example that reproduces the issue, that way we see the full code and can reproduce it locally and with any luck, you'll figure out the issue on your own, when reducing the code.