Start of total refactor

Almost total re-write to try and get around LUA limits