added a main loop, basic functionality now up

Also fixed some minor issues with left-over debug code and half-baked
refactoring.