Saftey fixes and minor cleanup