this block of logic is really really hard to understand, I give up :P