Refactored main simulation routine and split into numerous subroutines, ...

...cleaned up image updating also.