Lots of bug fixes and a few fixes to core functions.