It works now, needs to be optimized though