Updated with several bugfixes.