Implemented main loop. Closes #220.