Makefile added, previous single file split into many.
New functions, and at least partly refactored.
Code is still ugly in many places though (globals, copy/paste, bad naming, etc).