21.04.2016, 23:20
Before looking too in depth, the code seems to be rather unorganized and loop heavy. If I was in your position, I'd consider rewriting the way the system works. If you'd rather not rewrite it let me know and I'll take another look at it.