Fully working now. Might need some optimizations.