Purifying some ugly code