code cleanup + fixed program freeze when calulating image that is too large