Added a lot of new code. See commit log for more information.