Various bug fixes and additions