Initial version

C89 compliant. Needs testing and refactoring - everything is now in one huge
main function.