A couple bug fixes and code cleanup