final answer, its ugly but it works