fix math, remove ugly code