Various bug fixes and type corrections