adding more protections and refactors