Rewrote and fix almost everything. Works like a charm now.