this looks extremely complicated code for what it does? maybe worth breaking it to a smaller method to add some readability?