Refactoring and using SQL corrleation table

TODO: "using sql table"