restructed code. Separated graphics from computation