this is not exactly nice code to read.