Initial unfinished version, with a lot of redundant code I'm not brave enough to delete before having a backup of the whole code.