Tons of bugfix and modification