merged all the mess into one single function, this should make execution easier