Pla 1641

Added "ref" support as optional url parameter
WAIT FOR QA
This method is scary. Extracting smaller methods from it might help :)